# coding: utf-8 or # -*- coding: utf-8 -*-
import os
import json
import threading  
from flask import Flask
from xml.dom import minidom
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, scoped_session
from sqlalchemy import (create_engine, MetaData, Column, Integer, String)
from sqlalchemy import func
from sqlalchemy.sql.expression import and_, or_
from sqlalchemy import Table, Column, Integer, String, Text, DateTime, MetaData, ForeignKey, Boolean, types, Numeric
from datetime import datetime, date
from sqlca import get_Session
from json_exa import ret_json,row3dict
from login import get_worker
from get_cardinfo import get_card_info
from card_trade_confirm import confirm
from card_sale_day import sale_query
from card_check import check,print_ret,group_sum
from encrpty import EncrptyUtil
import urllib
import base64


app = Flask(__name__)

@app.route('/v1.0/login/<worker>', methods=['GET', 'POST'])
def login_yz(worker):
	#解密
	# ls_worker = EncrptyUtil().decrypt(worker)
	ls_worker = worker
	print ls_worker
	ls_worker = ls_worker.strip()
	ls_worker = ls_worker.replace(' ', '')
	ls_worker = ls_worker.lower()
	return get_worker(ls_worker)


@app.route('/v1.0/cardinfo/<infor>', methods=['GET', 'POST'])
def card_info(infor):
	# ls_infor = EncrptyUtil().decrypt(infor)
	print infor
	ls_infor = infor 
	ls_infor = ls_infor.strip()
	ls_infor = ls_infor.replace(' ', '')
	ls_infor = ls_infor.lower()
	return get_card_info(ls_infor)

# @app.route('/confirm/<infor>', methods=['GET'])
# def confirm_err(infor):
# 	ls_msg = "提交模式不允许,confirm接口参数较长请使用POST提交" 
# 	return json.dumps({"code": "200", "error": ls_msg}, ensure_ascii=False)

@app.route('/v1.0/confirm/<infor>', methods=['GET', 'POST'])
def confirm_req(infor):
	#ls_infor = EncrptyUtil().decrypt(infor)
	print infor
	ls_infor = infor
	ls_infor = ls_infor.strip()
	ls_infor = ls_infor.replace(' ', '')
	ls_infor = ls_infor.lower()
	ls_rt = confirm(ls_infor)
	return ls_rt

@app.route('/v1.0/query/<infor>', methods=['GET', 'POST'])
def sale(infor):
	#ls_infor = EncrptyUtil().decrypt(infor)
	ls_infor = infor
	ls_infor = ls_infor.strip()
	ls_infor = ls_infor.replace(' ', '')
	ls_infor = ls_infor.lower()
	return sale_query(ls_infor)


@app.route('/v1.0/check/<infor>', methods=['GET', 'POST'])
def check_infor(infor):
	#ls_infor = EncrptyUtil().decrypt(infor)
	ls_infor = infor
	print ls_infor
	ls_infor = ls_infor.strip()
	ls_infor = ls_infor.replace(' ', '')
	ls_infor = ls_infor.replace('/u', '')
	ls_infor = ls_infor.lower()
	return check(ls_infor)


@app.route('/v1.0/print/<infor>', methods=['GET', 'POST'])
def print_str(infor):
	#ls_infor = EncrptyUtil().decrypt(infor)
	ls_infor = infor
	ls_infor = ls_infor.strip()
	ls_infor = ls_infor.replace(' ', '')
	ls_infor = ls_infor.lower()
	return print_ret(ls_infor)


@app.route('/v1.0/summary/<infor>', methods=['GET', 'POST'])
def summaryv1(infor): 
	ls_infor = infor
	ls_infor = ls_infor.strip()
	ls_infor = ls_infor.replace(' ', '')
	ls_infor = ls_infor.lower()
	return group_sum(ls_infor)

#---------------------v2.0----------------------
@app.route('/v2.0/login/<worker>', methods=['GET', 'POST'])
def login_yz_v2(worker):
	ls_str = worker
	ls_worker = decode_http(ls_str)
	ls_worker = ls_worker.strip()
	ls_worker = ls_worker.replace(' ', '')
	ls_worker = ls_worker.lower()
	return get_worker(ls_worker) 


@app.route('/v2.0/cardinfo/<infor>', methods=['GET', 'POST'])
def card_info_v2(infor):
	ls_infor =decode_http(infor) 
	ls_infor = ls_infor.strip()
	ls_infor = ls_infor.replace(' ', '')
	ls_infor = ls_infor.lower()
	return get_card_info(ls_infor)

@app.route('/v2.0/confirm/<infor>', methods=['GET', 'POST'])
def confirm_req_v2(infor):
	#ls_infor = EncrptyUtil().decrypt(infor)
	print infor
	ls_infor =decode_http(infor) 
	ls_infor = ls_infor.strip()
	ls_infor = ls_infor.replace(' ', '')
	ls_infor = ls_infor.lower()
	ls_rt = confirm(ls_infor)
	return ls_rt

@app.route('/v2.0/query/<infor>', methods=['GET', 'POST'])
def sale_v2(infor):
	#ls_infor = EncrptyUtil().decrypt(infor)
	ls_infor =decode_http(infor) 
	ls_infor = ls_infor.strip()
	ls_infor = ls_infor.replace(' ', '')
	ls_infor = ls_infor.lower()
	return sale_query(ls_infor)


@app.route('/v2.0/check/<infor>', methods=['GET', 'POST'])
def check_infor_v2(infor):
	#ls_infor = EncrptyUtil().decrypt(infor)
	ls_infor =decode_http(infor) 
	print ls_infor
	ls_infor = ls_infor.strip()
	ls_infor = ls_infor.replace(' ', '')
	ls_infor = ls_infor.lower()
	
	return check(ls_infor)


@app.route('/v2.0/print/<infor>', methods=['GET', 'POST'])
def print_str_v2(infor):
	#ls_infor = EncrptyUtil().decrypt(infor)
	
	ls_infor =decode_http(infor) 
	ls_infor = ls_infor.strip()
	ls_infor = ls_infor.replace(' ', '')
	ls_infor = ls_infor.lower()
	return print_ret(ls_infor)



@app.route('/v2.0/summary/<infor>', methods=['GET', 'POST'])
def summaryv2(infor):
	ls_infor =decode_http(infor) 
	ls_infor = ls_infor.strip()
	ls_infor = ls_infor.replace(' ', '')
	ls_infor = ls_infor.lower()
	return group_sum(ls_infor)



def decode_http(ls_infor):
	ls_infor = ls_infor.encode('ascii')
	str_base = base64.urlsafe_b64decode(ls_infor)
	#str_base = urllib.unquote(ls_infor)
	str_base = str_base.decode('utf-8')
	print str_base
	ls_infor = EncrptyUtil().decrypt(str_base)
	return ls_infor
	
 


if __name__ == '__main__':
	app.run(host='127.0.0.1',debug=True)

# if __name__ == '__main__':
# 	app.run(host='10.10.10.18',debug=True)
 

# ls_infor ='eGp1YnZ6cGtCUHJpam4sd2l0YWVhemZBfiRxb3lzcyU8Izc3NTYlLiNvdmp2ZHZwcig_JjoyMTM7JzAlcnhkKD8mNTIzY2g-OjVjZDU_NTs4ZDo2Omc0OjM2Mmo3NzdkODAoMSZ1cW1lKD8mMzUjfQ=='
# str_base = base64.urlsafe_b64decode(ls_infor) 
# print str_base

# http://192.168.1.80:5000/v1.0/confirm/req_type=pay&posno=99999&old_bill =*&Key=867<25626<$53=34=2:.9213-70035&money=12&Track1=sfssdfssdfsf&track2=7181015575&track3=34343434
# # http://192.168.1.80:5000/v1.0/confirm/req_type=pay&posno=99999&old_bill =*&Key=867<25626<$53=34=2:.9213-70035&money=12&Track1=sfssdfssdfsf&track2=7181015575&track3=34343434
# infor ='req_type=pay&posno=99999&old_bill =*&Key=867<25626<$53=34=2:.9213-70035&money=12&Track1=sfssdfssdfsf&track2=7181015575&track3=34343434'
# infor = infor.lower()
# print confirm(infor)
# infor = 'req_type=sale&posno=99999&key=867<25626<$53=34=2:.9213-70035&dt=2015-10-08'
# print sale_query(infor)



# infor = 'req_type=pay&posno=7101&old_bill=*&key=867<257248$47=35=5:.9212-70035&money=1&Track1=&track2=6222020200039771825=49121206919991534&track3=996222020200039771825=156000000000000000000369199921600004912000000000000000000000=000000000000=00000000'
# infor = infor.lower()
# print confirm(infor)



# ls_infor = 'eGp1YnZ_dWlAcmJ-KnNxdG5zQDoyMDY0DCdvcmlhY2lycUErJnFqfUAyNjY6MDI5MjQ3Mjo_NDY8MTUwOzMxMDcvOjIwNjQobm90ans-MDQ9PSdUeGZnbjFDLyp3dGdobzU_Nj00NDIyMDQzMjI2PDwodXJnaG00PTc3NzoyNzc2Ozc8OjQzMA=='
# ls_infor = ls_infor.strip()
# ls_infor = ls_infor.replace(' ', '')
# ls_infor = ls_infor.lower()
# ls_infor = ls_infor.encode('ascii')
# str_base = base64.urlsafe_b64decode(ls_infor)
# str_base = str_base.encode('utf-8')
# print str_base
# ls_infor = EncrptyUtil().decrypt(str_base)
# ls_srt = decode_http(ls_infor)
# print confirm(ls_infor) 
